﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Media.Imaging;
using SuperFox.SuperServiceReference;
using System.Collections.ObjectModel;

namespace SuperFox
{
    public partial class clientProductDetails : UserControl
    {

        public static readonly DependencyProperty ProductProperty =
            DependencyProperty.Register("Product", typeof(DbProducts), typeof(clientProductDetails), new PropertyMetadata(new DbProducts(),
                                                new PropertyChangedCallback(ProductPropertyChanged)));


        public DbProducts Product
        {
            get { return (DbProducts)GetValue(ProductProperty); }
            set { SetValue(ProductProperty, value); }
        }

        private static void ProductPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            clientProductDetails cpd = (d as clientProductDetails);
            DbProducts p = e.NewValue as DbProducts;

            if (p == null)
                return;

            cpd.ProductName.Text = p.prod_name;
            cpd.ProductPrice.Text = "Price: $" + Decimal.Round(p.prod_price, 2) + " each";
            cpd.ProductDescription.Text = p.prod_longdescription;
            cpd.OptionsControl.Options = p.Options.Values;
            cpd.OptionsControl.OptionsName = p.Options.po_name;
            cpd.OptionsControl.Price = p.PriceTiers;

            cpd.ImageCanvas.Background = new ImageBrush()
            {
                ImageSource = new BitmapImage(new Uri(Config.PRODUCT_IMAGE_DIR + "/" + p.prod_largeimage, UriKind.RelativeOrAbsolute))
            };

            //cpd.ProductQuantity.Text = p.
        }

        public clientProductDetails()
        {
            InitializeComponent();
        }

        private void ProductRequired_KeyDown(object sender, KeyEventArgs e)
        {


        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            // the back button

            clientWelcomePage wp = new clientWelcomePage();

            wp.Products = (App.Current.RootVisual as MainPage).CurrentUser.Programs.Products;
            wp.ClientNameTb.Text = (App.Current.RootVisual as MainPage).CurrentUser.Programs.merc_name;

            (App.Current.RootVisual as MainPage).LoadContent(wp);
        }

        private void AddToCartButton_Click(object sender, RoutedEventArgs e)
        {
            // add to the cart

            DbUsers cu = (App.Current.RootVisual as MainPage).CurrentUser;

            if(cu == null)
            {
                // go back to login screen
                return;
            }

            if(cu.ShoppingCart == null)
            {
                cu.ShoppingCart = new ShoppingCartCollection();
            }

            // make sure the product isn't in the cart already

            if (!cu.ShoppingCart.Contains(this.DataContext as DbProducts))
            {

                cu.ShoppingCart.Add(this.DataContext as DbProducts);
            }
            // show shopping cart screen

            (App.Current.RootVisual as MainPage).LoadContent(new clientShoppingCart());

        }



        //private void ProductRequired_TextChanged(object sender, TextChangedEventArgs e)
        //{
        //    // make sure it can't exceed the maximum

        //    // TODO

        //    // calculate the total

        //    try
        //    {

        //        if ((sender as TextBox).Text == null || (sender as TextBox).Text == "" || (sender as TextBox).Text == string.Empty)
        //        {
        //            //this.TotalCost.Text = "0.00";
        //        }

        //        decimal test = (this.Product.prod_price * Int32.Parse((sender as TextBox).Text));

        //        test = Decimal.Round(test, 2);

        //        //this.TotalCost.Text = test.ToString();

        //    }
        //    catch (Exception)
        //    { }
        //}
    }
}
